package com.atguigu.school.controller;

import com.atguigu.school.commons.LoginForm;
import com.atguigu.school.service.SystemService;
import com.atguigu.school.commons.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/sms/system")
@Api(tags = "接收系统核心请求")
public class SystemController {

    /**
     * 处理系统核心业务
     */
    @Autowired
    private SystemService systemService;

    @ApiOperation("获取验证码图片")
    @GetMapping("/getVerifiCodeImage")
    public void getVerifiCodeImage(@ApiIgnore HttpServletRequest request, @ApiIgnore HttpServletResponse response) throws IOException {
        this.systemService.getVerifiCodeImage(request, response);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginForm loginForm,
                           @ApiIgnore HttpSession session) {
        return this.systemService.login(loginForm, session);
    }

    @GetMapping("/getInfo")
    @ApiOperation(value = "登录后凭借 token 获取用户信息")
    @ApiImplicitParam(name = "token", value = "token，通过请求头传递")
    public Result getInfoByToken(@RequestHeader("token") String token, @ApiIgnore HttpSession session) {
        return this.systemService.getInfoByToken(token, session);
    }

    @PostMapping("/headerImgUpload")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParam(name = "multipartFile", value = "预上传的文件")
    public Result headerImgUpload(
            @RequestPart("multipartFile") MultipartFile multipartFile,
            @ApiIgnore HttpServletRequest request) throws IOException {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        String originalFilename = multipartFile.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String newFileName = uuid.concat(originalFilename.substring(i));
        String directoryPath = request.getServletContext().getRealPath("/upload");
        File directory = new File(directoryPath);

        if (!directory.exists()) {
            directory.mkdirs();
        }

        File file = new File(directory, newFileName);

        if (!file.exists()) {
            file.createNewFile();
        }
        multipartFile.transferTo(file);
        String path = "upload/".concat(newFileName);
        return Result.ok(path);
    }

    @PostMapping("/updatePwd/{oldPwd}/{newPwd}")
    @ApiOperation(value = "管理员、学生、教师修改密码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "token", value = "登录成功时返回的 token"),
            @ApiImplicitParam(name = "oldPwd", value = "原密码"),
            @ApiImplicitParam(name = "newPwd", value = "新密码")
    })
    public Result updatePwd(@RequestHeader("token") String token, @PathVariable("oldPwd") String oldPwd, @PathVariable("newPwd") String newPwd) {
        return this.systemService.updatePwd(token, oldPwd, newPwd);
    }

}
